<p>
    <button onclick="geoFindMe()">Show my location</button>
</p>
<p>
    <input type="text" name="address" id="txtAddress" style="width: 50%">
    <label style="cursor: pointer" id="lblSearch">Search</label>
</p>

<div id="out"></div>
<div id="map-canvas"></div>
<script type="text/javascript">
    var bounds = new google.maps.LatLngBounds();
    var geocoder = new google.maps.Geocoder();
    var default_lat = parseFloat("<?php echo Yii::app()->params['default_lat']?>");
    var default_lng = parseFloat("<?php echo Yii::app()->params['default_lng']?>");
    var $map_wrapper = $("#map-canvas")[0];
    var base = "<?php echo HSUrl::home()?>";
    function initialize() {
        var mapOptions = {
            center: { lat: default_lat, lng: default_lng},
            zoom: 8,
            position: new google.maps.LatLng(default_lat, default_lng)
        };
        var map = new google.maps.Map($map_wrapper,
            mapOptions);
    }

    function initMap($cur_lat, $cur_lng) {
        $cur_lat = parseFloat($cur_lat);
        $cur_lng = parseFloat($cur_lng);

        var mapOptions = {
            center: { lat: $cur_lat, lng: $cur_lng },
            zoom: 15,
            position: new google.maps.LatLng($cur_lat, $cur_lng)
        };
        var map = new google.maps.Map($map_wrapper,
            mapOptions);
    }

    function getNearCompany($cur_lat, $cur_lng) {
        $cur_lat = parseFloat($cur_lat);
        $cur_lng = parseFloat($cur_lng);

        $.ajax({
            url: base + '/ajax/loadData',
            type: 'POST',
            dataType: 'json',
            data: {'lat': $cur_lat, 'lng': $cur_lng },
            success: function (data) {
                var mapOptions = {
                    center: { lat: $cur_lat, lng: $cur_lng},
                    zoom: 15,
                    position: new google.maps.LatLng($cur_lat, $cur_lng),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map($map_wrapper,
                    mapOptions);
                var infowindow = new google.maps.InfoWindow();

                var marker, $i;
                for ($i = 0; $i < data.results.length; $i++) {
                    var $item = data.results[$i];
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng($item['lat'], $item['lng']),
                        map: map
                    });

                    google.maps.event.addListener(marker, 'click', (function (marker, $i) {
                        return function () {
                            infowindow.setContent($item['name']);
                            infowindow.open(map, marker);
                        }
                    })(marker, $i));
                    bounds.extend(marker.position);
                }
                createInfoWindow('You are here', $cur_lat, $cur_lng, map);
//                google.maps.event.addDomListener(window, 'load', initialize());

            }
        });
    }

    function initMap($c, $a) {
        var mapOptions = {
            center: { lat: default_lat, lng: default_lng},
            zoom: 8,
            position: new google.maps.LatLng(default_lat, default_lng)
        };
        var map = new google.maps.Map($map_wrapper,
            mapOptions);
    }

    $(document).ready(function () {

        google.maps.event.addDomListener(window, 'load', initialize);

        $("#lblSearch").click(function () {
            var $address = $("#txtAddress").val().trim();
            if ($address == '') {
                alert('Vui lòng nhập địa chỉ');
            }
            getCoordinates($address);

        });
    });

    function getCoordinates(address) {
        geocoder.geocode({ 'address': address}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var $lng = results[0].geometry.location['D'];
                var $lat = results[0].geometry.location['k'];

                getNearCompany($lat, $lng);
            } else {
                return {'error': 'Không tìm được địa chỉ bạn yêu cầu.'}
            }
        }, function () {
            return {'error': 'Không tìm được địa chỉ bạn yêu cầu.'}
        });
    }

    function geoFindMe() {
        if (!navigator.geolocation) {
            alert("Geolocation is not supported by your browser");
            return;
        }

        function success(position) {

            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var pos = new google.maps.LatLng(position.coords.latitude,
                position.coords.longitude);

            var mapOptions = {
                content: 'Location found using HTML5.',
                position: pos,
                map: map,
                zoom: 15
            };
            var map = new google.maps.Map($map_wrapper,
                mapOptions);

            map.setCenter(pos);

            createMarker('You are here', latitude, longitude, map);
            google.maps.event.addDomListener(window, 'load', initialize);

        };

        navigator.geolocation.getCurrentPosition(success, error);
    }
    function createMarker(add, lat, lng, map) {
        var contentString = add;
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            map: map,
            content: 'You are here'
        });

        google.maps.event.addListener(marker, 'click', function () {
            var infowindow = new google.maps.InfoWindow();
            infowindow.setContent(contentString);
            infowindow.open(map, marker);
        });
        bounds.extend(marker.position);

    }

    function createInfoWindow(add, lat, lng, map) {

        var $option = {
            map: map,
            position: new google.maps.LatLng(lat, lng),
            content: 'Bạn đang ở đây'
        };
        var contentString = add;
        var infowindow = new google.maps.InfoWindow($option);

    }

    function error() {
        alert("Unable to retrieve your location");
    }

</script>